#include "stdafx.h"

#include "App.h"

App * App::msInstance = NULL;

App::App()
{
	msInstance = this;

    mhInst = NULL;
    mhWnd = NULL;
}

App::~App()
{
	msInstance = NULL;
}

bool App::Init()
{
    mSystem = new System();

    HWND hWnd = mhWnd;
    RECT rect;
    GetClientRect(hWnd, &rect);
    DWORD iWidth = rect.right - rect.left;
    DWORD iHeight = rect.bottom - rect.top;

    mSystem->Init();

	mRenderSystem = new GLRenderSystem(mhWnd);

	/*FileSystem fs("..\\Media");

	fs.Load();

	Archive::FileInfoVisitor fv = fs.GetFileInfos();

	while (!fv.eof())
	{
	TString128 filename = fv.Cursor()->second.name;

	if (FileHelper::GetExternName(filename) == "png")
	{
	TString128 dest = "D:\\Test\\" + FileHelper::GetBaseName(filename);
	TString128 src = "..\\Media\\" + filename;

	BOOL hr = ::CopyFile(src.c_str(), dest.c_str(), TRUE);

	int i = 0;
	}

	++fv;
	}*/

    return true;
}

void App::Update()
{
    System::Instance()->Run();

	RECT rc;
	GetClientRect(mhWnd, &rc);
	int w = rc.right - rc.left;
	int h = rc.bottom - rc.top;

	mRenderSystem->BeginScene();
	mRenderSystem->Clear(0xFFFF0000);

	mRenderSystem->EndScene();
}

void App::Shutdown()
{
	delete mRenderSystem;
	mSystem->Shutdown();
	delete mSystem;
}

bool App::Quit()
{
    return false;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMsg,WPARAM wParam,LPARAM lParam) 
{ 
    switch(iMsg)
    {
    case WM_CREATE:
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        break;
    }

    return ::DefWindowProc(hWnd, iMsg, wParam, lParam); 
}

#define WIN32_CLASS_NAME "Win32 Window For Game"

void App::Run(HINSTANCE hInstance)
{
    mhInst = hInstance;

    WNDCLASS wc;
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.hCursor= NULL; 
    wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION); 
    wc.hInstance = hInstance; 
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = WIN32_CLASS_NAME; 
    wc.lpszMenuName = NULL;
    wc.style = CS_HREDRAW | CS_VREDRAW;

	TCHAR tile[128] = "Window";
	DWORD style = WS_OVERLAPPEDWINDOW;
	POINT pt = { CW_USEDEFAULT, CW_USEDEFAULT };
	SIZE sz = { CW_USEDEFAULT, CW_USEDEFAULT };

    RegisterClass(&wc);

    HWND hWnd = CreateWindow(WIN32_CLASS_NAME, tile, style, 
        pt.x, pt.y, sz.cx, sz.cy,
        NULL, NULL, hInstance, NULL);

    ShowWindow(hWnd, SW_SHOW);

    mhWnd = hWnd;

    if (Init())
    {
        MSG msg;
        memset(&msg,0,sizeof(msg));
        while(msg.message != WM_QUIT && !Quit())
        {
            if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            else
            {
                Update();
            }
        }

        Shutdown();
    }

    UnregisterClass(WIN32_CLASS_NAME, hInstance);
}